package com.tr.rentcar.model.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.tr.rentcar.model.GlobalParamHandler;
import com.tr.rentcar.model.bean.Adminrole;
import com.tr.rentcar.model.bean.Admin_he;
import com.tr.rentcar.model.jdbc.MySqlDb;
import com.tr.rentcar.model.jdbc.ResultHandler;
import com.tr.rentcar.model.bean.Adminrole;

public class AdminDao implements ResultHandler<Admin_he>{
	private MySqlDb db=MySqlDb.getInstance();
	/**
	 * 查询所有
	 * @return
	 * @throws SQLException
	 */
	public List<Admin_he> findall() throws SQLException{
		String sql="select * from admin_role_admin";
		return db.executeQuery(sql, this);
	}
	/**
	 * 名称 用户名 性别 角色 电话、查寻
	 * @return
	 * @throws SQLException
	 */
	public List<Admin_he> findallByall(String or) throws SQLException{
		String sql="select * from admin_role_admin where ar_name=? or aname=? or asex=? or atel=? or anickname=?";
		return db.executeQuery(sql,  new GlobalParamHandler(or),this);
	}
	
	/**
	 * 增加一条记录
	 */
	public boolean save(String aname,String anickname,String apassword,String asex,String atel,Integer arid) throws SQLException{
		String sql="insert into admin(aname,anickname,apassword,asex,atel,ar_id) values(?,?,?,?,?,?)";
		return db.executeUpdate(sql, new GlobalParamHandler(aname,anickname,apassword,asex,atel,arid))>0;
	}
	/**
	 * 删除根据id一条记录
	 */
	public boolean admindeletebyid( Integer aid) throws SQLException {
		String sql = "delete from admin where aid=?";
		return db.executeUpdate(sql, new GlobalParamHandler( aid)) > 0;
	}
	
	/**
	 * 修改管理员资料
	 * @param uname
	 * @param utel
	 * @param uemail
	 * @param atel
	 * @param aid
	 * @return
	 * @throws SQLException
	 */
	public boolean updateadmin( String uname, String utel,String uemail,String atel,Integer arid,Integer aid) throws SQLException {
		String sql = "update  admin set aname=?,anickname=?,asex=?,atel=?,ar_id=? where aid=?";
		return db.executeUpdate(sql, new GlobalParamHandler(uname,utel,uemail,atel, arid,aid)) > 0;
	}
	/**
	 * 验证
	 * @param aname
	 * @param apassword
	 * @return
	 * @throws SQLException
	 */
	public Admin_he check(String aname,String apassword) throws SQLException{
		String sql="select * from admin_role_admin where aname=? and apassword=?";
		return db.queryUninResult(sql, new GlobalParamHandler(aname,apassword),this);
	}
	
	
	 
	public Admin_he doHander(Map<String,Object> row) {
		Admin_he ad=new Admin_he();
		ad.setAid(Integer.parseInt(row.get("aid")+""));
		ad.setAname((String)row.get("aname")+"");
		ad.setAnickname((String)row.get("anickname")+"");
		ad.setApassword((String)row.get("apassword")+"");
		ad.setAr_id(Integer.parseInt(row.get("ar_id")+""));
		ad.setAtel((String)row.get("atel")+"");
		ad.setAr_name((String)row.get("ar_name")+"");
		ad.setAsex((String)row.get("asex")+"");
		return ad;
	}

}
